<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use App\Response\Response;

class NobilityConfigController extends BaseController
{
    /**
     * Notes:获取爵位列表
     * User: wangyueli
     *
     * @param  Request  $request
     * @return string
     */
    public function nobilityList(Request $request)
    {
        $params = $request->input();
        $list = app('repo_nobility_config')->NobilityList($params);
        return Response::sendData($list);
    }

    /**
     * Notes:增加爵位
     * User: wangyueli
     *
     * @param  Request  $request
     * @return string
     * @throws BasicException
     */
    public function nobilityAdd(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'name' => 'required|string',
                'icon' => 'string',
                'picture' => 'string',
                'auth_ids' => 'string',
                'remark' => 'string',

                'level' => 'required|numeric|integer|min:0',
                'moods' => 'numeric|integer|min:0',
                'exp' => 'required|numeric|integer|min:0',
                'exp_keep' => 'required|numeric|integer|min:0',
                'ratio' => 'numeric|min:0',
            ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());


        $params = $request->all();
        //执行添加操作
        app('repo_nobility_config')->nobilityAdd($params);
        return Response::sendData(true);
    }

    /**
     * Notes:编辑爵位
     * User: wangyueli
     *
     * @param  Request  $request
     * @return string
     * @throws BasicException
     */
    public function nobilityEdit(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required',
                'name' => 'required|string',
                'icon' => 'string',
                'big_icon' => 'string',
                'picture' => 'string',
                'auth_ids' => 'string',
                'remark' => 'string',

                'level' => 'required|numeric|integer|min:0',
                'moods' => 'numeric|integer|min:0',
                'exp' => 'required|numeric|integer|min:0',
                'exp_keep' => 'required|numeric|integer|min:0',
                'ratio' => 'numeric|min:0',
            ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());
        //执行编辑操作

        $params = $request->all();
        //非必填赋值
        $params['icon'] = $params['icon'] ?? '';
        $params['picture'] = $params['picture'] ?? '';
        $params['auth_ids'] = $params['auth_ids'] ?? '';
        $params['remark'] = $params['remark'] ?? '';
        $params['moods'] = $params['moods'] ?? 0;
        $params['ratio'] = $params['ratio'] ?? 0;

        app('repo_nobility_config')->nobilityEdit($params);
        return Response::sendData(true);
    }

    /**
     * Notes:删除爵位和批量删除
     * User: wangyueli
     *
     * @param  Request  $request
     * @return string
     * @throws BasicException
     */
    public function nobilityDel(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $ids = $request->input('ids');
        $param = explode(',', $ids);
        //执行添加操作
        app('repo_nobility_config')->nobilityDel($param);
        return Response::sendData(true);
    }

    /**
     * Notes:获取爵位详情
     * User: wangyueli
     *
     * @param  Request  $request
     * @return string
     * @throws BasicException
     */
    public function nobilityInfo(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $id = $request->input('id');
        //执行查询操作
        $detail = app('repo_nobility_config')->nobilityDetail($id);
        return Response::sendData($detail);
    }

    /**
     * Notes:权限列表
     * User: wangyueli
     *
     * @return string
     */
    public function nobilityAuthList()
    {
        $list = app('repo_nobility_auth')->getNobilityAuthList();
        return Response::sendData($list);
    }

    /**
     * Notes:爵位权限添加
     * User: wangyueli
     *
     * @param  Request  $request
     * @return string
     * @throws BasicException
     */
    public function nobilityAuthAdd(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'name'        => 'required|string',
                'desc'        => 'required|string',
                'icon_bright' => 'string',
                'icon_grey'   => 'string',
                'status'      => 'required|integer|in:0,1',
                'sort'        => 'integer',
                'product_ids' => 'required|string',
            ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());


        $params = $request->all();

        app('repo_nobility_auth')->nobilityAuthAdd($params);
        return Response::sendData(true);
    }

    /**
     * Notes:编辑权限
     * User: wangyueli
     *
     * @param  Request  $request
     * @return string
     * @throws BasicException
     */
    public function nobilityAuthEdit(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id'        => 'required|integer',
                'name'        => 'required|string',
                'desc'        => 'required|string',
                'icon_bright' => 'string',
                'icon_grey'   => 'string',
                'status'      => 'required|integer|in:0,1',
                'sort'        => 'integer',
                'product_ids' => 'required|string',
            ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());


        $params = $request->all();

        //非必填渲染数据
        $params['icon_bright'] = $params['icon_bright'] ?? '';
        $params['icon_grey'] = $params['icon_grey'] ?? '';
        $params['sort'] = $params['sort'] ?? 99999;


        app('repo_nobility_auth')->nobilityAuthEdit($params);
        return Response::sendData(true);
    }

    /**
     * Notes:删除权限
     * User: wangyueli
     *
     * @param  Request  $request
     * @return string
     * @throws BasicException
     */
    public function nobilityAuthDel(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $ids = $request->input('ids');
        $params = explode(',', $ids);

       app('repo_nobility_auth')->nobilityAuthDel($params);
        return Response::sendData(true);
    }

    /**
     * Notes:获取权限下拉菜单
     * User: wangyueli
     *
     * @return string
     */
    public function getAuthSelect()
    {
        $list = app('repo_nobility_auth')->getAuthSelect();

        return Response::sendData($list);
    }








}
